########################################################################
#  Searx-qt - Lightweight desktop application for SearX.
#  Copyright (C) 2020  CYBERDEViL
#
#  This file is part of Searx-qt.
#
#  Searx-qt is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Searx-qt is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
########################################################################

from PyQt5.QtCore import QObject, pyqtSignal

from searxqt.models.instances import Instances


class SettingsModel(QObject):
    changed = pyqtSignal()

    def __init__(self, parent=None):
        QObject.__init__(self, parent=parent)

        self._requests = RequestSettingsModel()
        self._stats2 = SearxStats2Model()

        self._requests.changed.connect(self.changed)

    def loadSettings(self, data):
        requestsData = data.get('requests', {})
        self.requests.data = {
            'verify': requestsData.get('verify', True),
            'timeout': requestsData.get('timeout', 10),
            'proxies': requestsData.get('proxies', {})
        }

        stats2Data = data.get('stats2', {})
        self.stats2.loadSettings(stats2Data)

    def saveSettings(self):
        return {
            'requests': self.requests.data,
            'stats2': self.stats2.saveSettings()
        }

    @property
    def requests(self): return self._requests

    @property
    def stats2(self): return self._stats2


class SearxStats2Model(QObject):
    changed = pyqtSignal()

    def __init__(self, parent=None):
        QObject.__init__(self, parent=parent)
        self._backupUrl = Instances.URL

    def loadSettings(self, data):
        self.url = data.get('url', self._backupUrl)

    def saveSettings(self):
        return {
            'url': self.url
        }

    def reset(self):
        self.url = self._backupUrl

    @property
    def url(self):
        return Instances.URL

    @url.setter
    def url(self, url):
        Instances.URL = url
        self.changed.emit()


class RequestSettingsModel(QObject):
    changed = pyqtSignal()

    def __init__(self, parent=None):
        QObject.__init__(self, parent=parent)
        self._kwargs = {
            'verify': True,
            'timeout': 10,
        }

    def __changed(self): self.changed.emit()

    @property
    def data(self): return self._kwargs

    @data.setter
    def data(self, data):
        self._kwargs = data

    @property
    def verify(self): return self._kwargs['verify']

    @verify.setter
    def verify(self, state):
        self._kwargs['verify'] = state
        self.__changed()

    @property
    def timeout(self): return self._kwargs['timeout']

    @timeout.setter
    def timeout(self, state):
        self._kwargs['timeout'] = state
        self.__changed()

    @property
    def proxies(self): return self._kwargs.get('proxies', {})

    @proxies.setter
    def proxies(self, data):
        self._kwargs.update({'proxies': data})
        self.__changed()
